// 地图页面交互功能 - 适配高德地图

document.addEventListener('DOMContentLoaded', function() {
    // 设置搜索功能
    setupSearch();
    
    // 设置附近景点点击事件
    setupNearbyItems();
    
    // 设置底部导航
    setupBottomNavigation();
});

// 搜索功能
function setupSearch() {
    const searchInput = document.querySelector('input[type="text"]');
    const searchIcon = document.querySelector('.fa-search');
    
    if (searchInput) {
        searchInput.addEventListener('focus', function() {
            if (this.parentElement) {
                this.parentElement.style.transform = 'scale(1.02)';
            }
        });
        
        searchInput.addEventListener('blur', function() {
            if (this.parentElement) {
                this.parentElement.style.transform = 'scale(1)';
            }
        });
        
        searchInput.addEventListener('input', function() {
            const query = this.value.toLowerCase();
            if (query.length > 0) {
                // 调用高德地图搜索功能
                if (typeof searchAttraction === 'function') {
                    searchAttraction(query);
                }
            }
        });
        
        // 回车搜索
        searchInput.addEventListener('keypress', function(e) {
            if (e.key === 'Enter' && typeof searchAttraction === 'function') {
                searchAttraction(this.value);
            }
        });
    }
    
    if (searchIcon) {
        searchIcon.addEventListener('click', function() {
            if (searchInput) searchInput.focus();
        });
    }
}

// 附近景点点击事件
function setupNearbyItems() {
    const nearbyItems = document.querySelectorAll('.nearby-item');
    
    nearbyItems.forEach(item => {
        item.addEventListener('click', function() {
            const name = this.querySelector('h4').textContent;
            
            // 调用高德地图功能定位到景点
            if (typeof searchAttraction === 'function') {
                searchAttraction(name);
            }
        });
    });
}

// 底部导航
function setupBottomNavigation() {
    const navItems = document.querySelectorAll('.fixed.bottom-0 .text-center');
    
    navItems.forEach((item, index) => {
        item.addEventListener('click', function() {
            // 移除所有活动状态
            navItems.forEach(nav => {
                const icon = nav.querySelector('i');
                const text = nav.querySelector('p');
                if (icon) {
                    icon.classList.remove('text-green-600');
                    icon.classList.add('text-gray-400');
                }
                if (text) {
                    text.classList.remove('text-green-600');
                    text.classList.add('text-gray-400');
                }
            });
            
            // 添加当前项的活动状态
            const currentIcon = this.querySelector('i');
            const currentText = this.querySelector('p');
            if (currentIcon) {
                currentIcon.classList.remove('text-gray-400');
                currentIcon.classList.add('text-green-600');
            }
            if (currentText) {
                currentText.classList.remove('text-gray-400');
                currentText.classList.add('text-green-600');
            }
            
            // 页面跳转
            const pages = ['home.html', 'map.html', 'ar.html', 'shop.html', 'profile.html'];
            if (index < pages.length) {
                // 延迟跳转，让用户看到状态变化
                setTimeout(() => {
                    window.location.href = pages[index];
                }, 200);
            }
        });
    });
}